// pages/goodsDetail/goodsDetail.js
const app = getApp()
var host = app.globalData.host;
Page({
    data: {
        host: host,
        vertical: false,
        autoplay: true,
        circular: true,
        interval: 2000,
        swiperCurrent: 0,
        duration: 500,
        imgUrls: [],
        currentTab: 0,
        goodsDetail: null,
        num: 0,
        cartNum: 0,
        goodsId: '1',

    },
    swiperChange: function(e) {
        // console.log(e)
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
    // 实现切换导航栏
    switchNav: function(e) {
        console.log(e)
        console.log("currentTab的当前值为：" + e.currentTarget.id)
        console.log("current当前值为：" + e.currentTarget.id)
            // console.log("图片列表：" + e.goodsDetail.roundPlayPicList)

        var that = this;

        if (this.data.currentTab == e.currentTarget.id) {

            return false;
        } else {
            that.setData({ currentTab: e.currentTarget.id });


        }

    },
    addGoods: function(e) {
        // console.log(e)
        var num = this.data.num;
        var cartNum = this.data.cartNum;
        // console.log(num)
        if (num >= 0) {
            this.setData({

                num: num + 1,
                // cartNum:num+1 ,


            });
            console.log(num)
                // console.log(cartNum)


        }
    },
    minusGoods: function(e) {
        var num = this.data.num;
        var cartNum = this.data.cartNum;
        if (num > 0) {
            this.setData({

                num: num - 1,
                // cartNum:num-1 ,

            });
            console.log(num)
            console.log(cartNum)

        }
    },


    onLoad: function(e) {
        console.log(e)
            // var goodsId = '1'

        var goodsId = e.goodsId
        this.loadGoodsDetail(goodsId);
        this.loadCart();
    },
    loadGoodsDetail: function(goodsId) {
        if (goodsId != "") {
            var that = this;
            wx.request({
                url: host + '/api/goods/getGoodsDetail',
                data: { "goodsId": goodsId },
                // 写死id,就可以直接在goodsDetail页面编译
                // data: { "goodsId": 2 },
                header: { 'Content-Type': 'application/json' },
                method: 'GET',
                success: function(res) {
                    console.log(res)
                    var goodsDetail = res.data.data;
                    that.setData({
                        goodsDetail: goodsDetail,
                        imgUrls: goodsDetail.roundPlayPicList,
                    });
                    // console.log(goodsDetail)
                }
            })
        }
    },
    seeCart: function(e) {
        console.log(e)
        wx.switchTab({
            url: '../cart/cart',
        })
    },
    intocart: function(e) {
        console.log(e)
        var that = this;
        var goodsId = e.currentTarget.id;
        var userId = wx.getStorageSync("userId");
        console.log(userId)
        if (userId != "") {
            wx.request({
                url: host + '/api/cart/saveShoppingCart',
                method: 'GET',
                data: {
                    userId,
                    goodsId,
                    'type': '0'
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    var code = res.data.code;
                    if (code == '0000') {
                        that.loadCart();
                    }

                }
            })
        } else {
            wx.redirectTo({
                url: '../login/login'
            })
        }

    },
    loadCart: function() {
        var that = this;
        var userId = wx.getStorageSync("userId");
        if (userId != "") {
            wx.request({
                url: host + '/api/cart/getShoppingCartList',
                method: 'GET',
                data: {
                    // userId,
                    "userId": userId,
                    'type': '0'

                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res);
                    var code = res.data.code;
                    if (code == '0000') {
                        var ret = res.data.data;
                        that.setData({
                            cartNum: ret.length

                        });
                    }

                }
            })
        } else {
            wx.redirectTo({
                url: '../login/login'
            })
        }
    },
    buy: function() {
        var goodsId = this.data.goodsId;
        var userId = wx.getStorageSync("userId");
        if (goodsId == '' || goodsId == null) {
            wx.showModal({
                title: '提示',
                content: '请选择结算商品',
                showCancel: true
            })
        } else {
            wx.navigateTo({
                url: '../buy/buy?goodsId=' + goodsId + '&num' + this.data.num
            })
        }
    }

})